<?php
include "header.php";
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
	if (isset($_GET["id"]) && $_GET["id"] !== ""){
		$id = $mysqli->real_escape_string(trim($_GET["id"]));
		if ($id != 0){
			$query = "SELECT name, number FROM Groups WHERE g_id = ?;";
			$stmt = $mysqli->stmt_init();
			if ($stmt = $mysqli->prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute();
				$stmt->bind_result($name, $number);
				if (!($stmt->fetch())){
					echo "<script>window.location.href='managegroups.php'</script>";
				}
				$stmt->close();
			}
			$query = "SELECT Courses.c_id, Courses.prefix, Courses.number, Courses.title FROM Course_Group INNER JOIN Courses WHERE (g_id = ?) AND (Course_Group.c_id = Courses.c_id) ORDER BY Courses.c_id;";
			$stmt = $mysqli->stmt_init();
			if ($stmt = $mysqli->prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute();
				$stmt->bind_result($c_id, $prefix, $number, $title);
			}
			$button_name = "Update";
		}
		else{
			$button_name = "Add";
		}
	}
	else{
		$id = 0;
		$button_name = "Add";
	}
?>
<script>change_picture("admin");</script>
<script>
var list = new Array();
var currentPage = 0;
function remove_course(id){
	var index = list.indexOf(id);
	if (index > -1) {
		list.splice(index, 1);
		$("#course_"+id).remove();
	}
	search_course(currentPage);
}
function add_course(id, prefix, number, title){
	var index = list.indexOf(id);
	if (index < 0) {
		list.push(id);
		$("#course_list").append("<tr id='course_"+id+"'><td>"+prefix+"</td><td>"+number+"</td><td>"+title+"</td><td><a href = '#' onclick = 'remove_course("+id+");false;'>Remove</a></td></tr>");
	}
	search_course(currentPage);
}
function search_course(n){
	$.post("findcourses.php",
	{
		prefix:$("#search_prefix").val(),
		number:$("#search_number").val(),
		title:$("#search_title").val(),
		course_list:list,
		page:n,
	},
	function(data,status){
		$("#search_result").html(data);
	});
}
function submit_form(){
	$('#input_list').val(list);
	$('#updategroups_form').submit();
}
</script>
<div>
<form id = "updategroups_form" action = "updategroups.php" method = "POST"/>
<input type = "hidden" name = "g_id" value = "<?php echo $id; ?>" />
<label for = "name">Name: </label><input type = "text" name = "name" value = "<?php echo $name; ?>" /><br />
<label for = "number">Required Number: </label><input type = "text" name = "number" value = "<?php echo $number; ?>" /><br />
<input type = "hidden" name = "course_list" id = "input_list" />
<div>Course List:</div>
<div><a href = "#" onclick = "$('#search_list').show();false;">Add a course</a></div>
<div> 
<table id="course_list">
<?php
	if (isset($stmt)){
		while ($stmt->fetch()){
			echo "<tr id='course_" . $c_id . "'><td>" . $prefix . "</td><td>" . $number . "</td><td>" . $title . "</td><td><a href = '#' onclick = 'remove_course(" . $c_id . ");false;'>Remove</a></td></tr>";
			echo "<script>list.push(" . $c_id . ");</script>";
		}
		$stmt->close();
	}
?>
</table>
</div>
<input type = "button" value = "Back" onclick = "window.location.href='managegroups.php'" />
<input type = "button" value = "<?php echo $button_name;?>" onclick = "submit_form();" />
</form>
</div>
<div id = "search_list" style = "display:none;">
<div>
<input type = "button" value = "Close" onclick = "$('#search_list').hide();false;" />
</div>
<div>
<label for = "prefix">Prefix: </label><input type = "text" name = "prefix" id = "search_prefix" />
<label for = "c_number">Number: </label><input type = "text" name = "c_number" id = "search_number" />
<label for = "title">Title: </label><input type = "text" name = "title" id = "search_title" />
<input type = "button" value = "Search" onclick = "search_course(0);false;"/>
</div>
<div id = "search_result"></div>
</div>
<?php
}
include "footer.php";